import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

import plugins_request from "@/plugin/request.js"
let api = plugins_request.api;
let httpApi = plugins_request.httpApi;

// 处理产品分类数据
function handle_product_cate_data(category_tree) {
  let root_route = "/product-cates?ids="; //配件产品

  let category_flat = [];
  category_tree.forEach((level1) => {
    level1.level = "1";
    level1.idlist = [level1.id];
    level1.ids = level1.idlist.join("-");
    level1.route = root_route + level1.ids;

    category_flat.push({
      ...level1,
    });

    let list_cates_level2 = level1.channels; //.filter(v => v.is_show || 1);
    if (list_cates_level2.length) {
      list_cates_level2.forEach((level2) => {
        level2.level = "2";
        level2.idlist = [level1.id, level2.id];
        level2.ids = level2.idlist.join("-");
        level2.route = root_route + level2.ids;

        category_flat.push({
          ...level2,
        });

        let list_cates_level3 = level2.channels; //.filter(v => v.is_show || 1);
        if (list_cates_level3.length) {
          list_cates_level3.forEach((level3) => {
            level3.level = "3";
            level3.idlist = [level1.id, level2.id, level3.id];
            level3.ids = level3.idlist.join("-");
            level3.route = root_route + level3.ids;

            category_flat.push({
              ...level3,
            });
          });
        }
      });
    }
  });

  return {
    category_flat: category_flat,
    category_tree: category_tree,
  };
}

//
export default new Vuex.Store({
  state: {
    vuex_h5: !!navigator.userAgent.match(
      /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
    ),
    vuex_avatar_default: require("@img/common/avatar.png"),
    vuex_user: {},
    //
    vuex_config: {},
    vuex_news_cates: [],
    //
    vuex_is_login: localStorage.getItem('token')?true:false, //是否登录
    //
    vuex_cart_number: 0,
    vuex_cart_product:[],
    vuex_huobi: "￥",
    //
    vuex_category_tree: [],
    vuex_category_flat: [],
    //
    default_address: {}, //默认收货地址
    //
    vuex_index_banners: [],
    vuex_map_banners: {
      关于我们: [],
      联系我们: [],
    },
    //
  },

  getters: {},

  mutations: {
    //设置数据
    set_vuex_data(state, obj) {
      let key = obj.key;
      let val = obj.val;
      state[key] = val;
    },
    set_vuex_config(state, data) {
      state.vuex_config = data;
    },

    //设置基本信息
    set_vuex_user(state, data) {
      // //console.log("vuex set_vuex_user 设置用户信息", { ...data });

      let token = data.token;
      let userId = data.userId || data.id;

      state.vuex_is_login = true;
      state.token = token;
      state.userId = userId;
      state.vuex_user = data;

      localStorage.setItem("token", token);
      localStorage.setItem("userId", userId);
      localStorage.setItem("vuex_user", JSON.stringify(data));
    },

    //设置购物车商品数量
    set_vuex_cart_number(state, value) {
      state.vuex_cart_number = value;
    },

    set_cache_payment_products(state, str_products) {
      console.log('vuex 缓存商品信息', str_products)
      sessionStorage.setItem("cache_payment_products", str_products);
    },

    set_vuex_banner(state, data) {
      let [pos_0, pos_1, pos_2] = data;
      console.log("首页轮播", pos_0.images);
      state.vuex_index_banners = pos_0.images;

      state.vuex_map_banners = {
        关于我们: pos_1.images,
        联系我们: pos_2.images,
      };
    },
    // 产品分类树
    set_vuex_product_cate(state, data) {
      let { category_flat, category_tree } = data;
      console.warn(
        "category_tree 产品分类数据",
        JSON.parse(JSON.stringify(category_tree))
      );

      state.vuex_category_tree = category_tree;
      state.vuex_category_flat = category_flat;
    },

    set_vuex_login_status(state, value) {
      // //console.log("--------------- 用户是否登录 ---------------", value);
      state.vuex_is_login = value;
    },
    //清空登录信息
    remove_vuex_user(state) {
      state.vuex_cart_number = "0";
      state.token = "";
      state.userId = "";
      state.vuex_user = {};
      state.vuex_is_login = false;
      localStorage.clear();
    },

  },

  actions: {
    async appInit({ commit, state, dispatch }, data) {
      // dispatch("query_assets");

      let token = localStorage.getItem("token");
      // let userId = localStorage.getItem("userId");

      // if (token && userId) {
      //   let cache_user = localStorage.getItem("vuex_user");
      //   commit("set_vuex_user", cache_user);
      //   dispatch("query_user_auth_info");
      // } else {
      //   commit("remove_vuex_user");
      // }
      if(token) {
        dispatch("query_cart");
      }
    },

    //获取登录后的信息
    async query_user_auth_info({ commit, state, dispatch }, data) {
      dispatch("query_user");
      dispatch("query_cart");
    },

    // 获取用户信息
    async query_user({ commit, state, dispatch }) {
      api({
        url: "/service.php",
        method: "get",
        data: {
          action: "users_userInfo",
        },
      }).then((res) => {
        if (res.code == 200) {
          commit("set_vuex_user", res.data);
        } else {
          commit("remove_vuex_user");
        }
      });
    },
    // 购物车
    async query_cart({ commit, state, dispatch }) {
      console.log("vuex 查询购物车");
      httpApi("/api/v1/purchaser/cart/list", {}, "get").then((res) => {
        let { code, data } = res;
        if (code == 200) {
          state.vuex_cart_product = data;
          state.vuex_cart_number = data.length;
        }
      });
    },

    //初始化资源
    async query_assets({ commit, state, dispatch }, data) {
      dispatch('query_config')
      dispatch('query_banner')
      dispatch('query_category')
      dispatch('query_news')
    },

    // 查询
    async query_config({ commit, state, dispatch }) {
      api({
        url: "/service.php",
        method: "get",
        data: {
          action: "index_config",
        },
      }).then((res) => {
        if (res.code == 200) {
          let data = res.data;
          commit("set_vuex_config", data);

          if (data.comLogo && document) {
            document
              .querySelector('meta[property="og:image"]')
              .setAttribute("content", data.comLogo);
          }
        }
      });
    },
    // 查询
    async query_banner({ commit, state, dispatch }) {
      api({
        url: "/service.php",
        method: "get",
        data: {
          action: "banner_index",
          position: 0, //服务端：0-全部 1-通用 2-PC 3-H5 4-小程序 5-APP
        },
      }).then((res) => {
        if (res.code == 200) {
          commit("set_vuex_banner", res.data);
        }
      });
    },
    // 查询
    async query_category({ commit, state, dispatch }) {
      api({
        url: "/service.php",
        method: "get",
        data: {
          action: "product_channel",
          parentId: 0,
        },
      }).then((res) => {
        if (res.code == 200) {
          let catesInfo = handle_product_cate_data(res.data);
          commit("set_vuex_product_cate", catesInfo);
        }
      });
    },
    // 查询
    async query_news({ commit, state, dispatch }) {
      api({
        url: "/service.php",
        method: "get",
        data: {
          action: "news_channel",
          channelId: 49
        },
      }).then((res) => {
        if (res.code == 200) {
          let data = res.data
          data.forEach((v) => {
            v.route = "/news?id=" + v.id;
          });
          commit("set_vuex_data", {
            key: "vuex_news_cates",
            val: data,
          });
        }
      });
    },


  },
});
